package com.example.hehecat.yuejian1.ui.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.ActivityInfoContract;
import com.example.hehecat.yuejian1.mvp.model.ActivityInfoModel;
import com.example.hehecat.yuejian1.mvp.model.api.Api;
import com.example.hehecat.yuejian1.mvp.model.bean.ActivityBean;
import com.example.hehecat.yuejian1.mvp.presenter.ActivityInfoPresenter;
import com.example.hehecat.yuejian1.ui.activity.ReplyActivityActivity;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;
import com.ldoublem.thumbUplib.ThumbUpView;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Subscriber;

import static com.chad.library.adapter.base.listener.SimpleClickListener.TAG;

public class ActivityInfoFragment extends SupportFragment implements ActivityInfoContract.View {

    @BindView(R.id.title)
    AppCompatTextView title;
    @BindView(R.id.tpv)
    ThumbUpView tpv;
    @BindView(R.id.content)
    AppCompatTextView content;
    @BindView(R.id.share)
    Button share;
    @BindView(R.id.enroll)
    Button enroll;
    Unbinder unbinder;
    int activity_id;
    int user_id;
    @BindView(R.id.activity_img)
    ImageView activityImg;
    @BindView(R.id.activity_start)
    TextView activityStart;
    @BindView(R.id.activity_end)
    TextView activityEnd;

    ActivityInfoPresenter presenter;

    @BindView(R.id.TextView)
    android.widget.TextView TextView;
    @BindView(R.id.bt_reply)
    Button btReply;

    @OnClick(R.id.activityinfo_return)
    void gameReturn() {
        MainFragment fragment = new MainFragment();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, fragment, null)
                .commit();
    }

    @OnClick(R.id.share)
    void btShareOnClick() {
        Intent textIntent = new Intent(Intent.ACTION_SEND);
        textIntent.setType("text/plain");
        textIntent.putExtra(Intent.EXTRA_TEXT, content.getText());
        startActivity(Intent.createChooser(textIntent, "分享"));
    }

    @OnClick(R.id.enroll)
    void btEnrollClick() {
        presenter.activityEnroll(activity_id, user_id);
    }

    @OnClick(R.id.bt_reply)
    void btReplyClick() {
        Intent intent = new Intent(getContext(), ReplyActivityActivity.class);
        intent.putExtra("activity_id", activity_id);
        startActivity(intent);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activity_info, container, false);
        presenter = new ActivityInfoPresenter(this);
        unbinder = ButterKnife.bind(this, view);

        if (getArguments() != null) {
            activity_id = getArguments().getInt("activity_id");
        }
        user_id = MyStaticVariable.newInstance().getUserid();
        Log.d(TAG, "onCreateView:activity_id " + activity_id);


        //点赞
        isActivityCollect();
        tpv.setUnLikeType(ThumbUpView.LikeType.broken);
        tpv.setCracksColor(Color.rgb(22, 33, 44));
        tpv.setFillColor(Color.rgb(11, 200, 77));
        tpv.setEdgeColor(Color.rgb(33, 3, 219));
        tpv.setOnThumbUp(new ThumbUpView.OnThumbUp() {
            @Override
            public void like(boolean like) {
                collectActivity();
            }
        });


        //获取详情
        ActivityInfoModel model = new ActivityInfoModel();
        model.getActivity(activity_id, new Subscriber<ActivityBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ActivityBean activityBean) {
                Log.d(TAG, "onNext: " + activityBean.getActivityname());
                activityStart.setText(activityBean.getStartdate() + "至");
                activityEnd.setText(activityBean.getEnddate());
                content.setText(activityBean.getIntro());
                Picasso.with(getContext()).load(Api.IMG_URL + activityBean.getActivitypic()).into(activityImg);
            }
        });
        return view;
    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setCollect() {
        tpv.setLike();
    }

    @Override
    public void setUnCollect() {
        tpv.setUnlike();
    }

    @Override
    public void collectActivity() {
        presenter.collectActivity(activity_id, user_id);
    }

    @Override
    public void isActivityCollect() {
        presenter.isActivityCollect(activity_id, user_id);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public static ActivityInfoFragment newInstance(int activity_id) {
        ActivityInfoFragment fragment = new ActivityInfoFragment();
        Bundle args = new Bundle();
        args.putInt("activity_id", activity_id);
        fragment.setArguments(args);
        return fragment;
    }
}
